package com.alameda.listener;

import com.alameda.base.constant.ProjectConstant;
import com.alameda.commonpent.event.message.EventMessage;
import com.alameda.listener.strategy.api.ListenerStrategy;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 移动端服务事件监听器
 *
 * @author Lv
 * @date 2025/1/4
 */
@Slf4j
@Component
public class EventListener {

    @Autowired
    private Map<String, ListenerStrategy> strategyMap;

    /**
     * 监听事件
     *
     * @param eventMessage 事件消息对象
     */
    @SuppressWarnings("all")
    @org.springframework.context.event.EventListener
    public void onEvent(EventMessage eventMessage) {
        String message = eventMessage.getMessage();
        Map<String, Object> map = JSONObject.parseObject(message, Map.class);

        // 获取策略
        String strategyKey = (String) map.get(ProjectConstant.StrategyConstant.LISTEN_STRATEGY);
        ListenerStrategy listenerStrategy = strategyMap.get(strategyKey);
        listenerStrategy.listenEvent(map);
    }
}
